#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "conf.h"
#include "dbHandler/dbHandler.h"
#include "svstub.h"
#include <unistd.h>
#include "../clsv/clsv.h"


extern char *optarg;
extern int optind, opterr, optopt;


/* Gets the configuration for the server from a file.
 * If no IP is specified in the configuration file, ip is set to NULL.
 * It returns the number of ports the server must listen, or less than zero if
 * an error has occured */
int
getConf(char * file, int * port, char * ip, int * connections, char * dbname,
		int * cap)
{
	FILE * f;
	int n;
	char c;
	char par[MAX_PAR], val[MAX_VAL];
	int nports = 0, nip = 0;
	
	if ((f = fopen(file, "r")) == NULL)
	{
		fprintf(stderr, "Error opening configuration file\n");
		return -1;
	}
	
	/* get all the initial comment */
	while ((c = fgetc(f)) != '#');
	
	for (n = 2; n == 2 && (n = fscanf(f, "%s = %s\n", par, val)) != EOF; )
	{
		/* port parsed */
		if (strcmp(CONF_PORT, par) == 0)
			port[nports++] = atoi(val);
		
		/* IP parsed */
		else if (strcmp(CONF_IP, par) == 0)
		{
			strcpy(ip, val);
			nip++;
		}
		
		/* connections parsed */
		else if (strcmp(CONF_CONN, par) == 0)
			*connections = atoi(val);
		
		/* database name parsed */
		else if (strcmp(CONF_DB, par) == 0)
			strcpy(dbname, val);
		
		/* server's capacity parsed */
		else if (strcmp(CONF_CAP, par) == 0)
			*cap = atoi(val)*1024*1024;
		
		else
		{
			n = 0;
			fprintf(stderr, "Configuration file is malformed\n");
			return -1;
		}
	}
	
	/* no IP specified in configuration file */
	if (nip == 0)
		strcpy(ip, DEFAULT_HOST);
	
	return nports;
}

int
parseConf(int argc, char * argv[], int * port, char * ip, int * connections,
		  char * dbname, int * cap)
{
	int i = 0;
    int c, conn = 0, capp = 0;
    int iarg = 0, parg = 0, carg = 0, darg = 0, marg = 0;
     
    opterr = 0;
    *connections = 0;
    /* get options and their arguments*/
	while ((c = getopt (argc, argv, "i:p:c:d:m:")) != -1)
    	switch (c)
        {
			case 'i':	if(strlen(optarg) < MAX_IP_LEN)
						{
             				strcpy(ip, optarg);
             				iarg = 1;
						}
						else
						{
							fprintf (stderr, "Ip specified is too long.\n");
               				return 0;
             			}
             			break;
			case 'p':	if(i < MAX_PORTS)
             			{
             				port[i++] = atoi(optarg);
             				parg = 1;
             			}
             			else
             			{
             				fprintf (stderr, "Too many ports specified.\n");
               				return 0;
             			}
             			break;
           	case 'c':
           				if ((conn = atoi(optarg)) > 0)
           				{
           					*connections = conn;
           					carg = 1;
           				}
           				else
           				{
           					fprintf (stderr, "Wrong number of connections.\n");
               				return 0;
           				}
             			break;
           	case 'd':	
           				if(strlen(optarg) < MAX_FILENAME)
             			{
             				strcpy(dbname, optarg);
             				darg = 1;
             			}
             			else
             			{
             				fprintf (stderr, "DB filename too long.\n");
             				return 0;
             			}
             			break;
            case 'm':
           				if ((capp = atoi(optarg)) > 0)
           				{
           					*cap = capp;
           					marg = 1;
           				}
           				else
           				{
           					fprintf (stderr, "Wrong number of capacity.\n");
               				return 0;
           				}
             			break;
			case '?':
            			if (optopt == 'i')
               			fprintf (stderr, "Option -%c requires an argument.\n",
               					 optopt);
               			else if (optopt == 'p')
               			fprintf (stderr, "Option -%c requires an argument.\n",
               					 optopt);
               			else if (optopt == 'c')
               			fprintf (stderr, "Option -%c requires an argument.\n",
               					 optopt);
               			else if (optopt == 'd')
               			fprintf (stderr, "Option -%c requires an argument.\n",
               					 optopt);
               			else if (optopt == 'm')
               			fprintf (stderr, "Option -%c requires an argument.\n",
               					 optopt);
             			else if (isprint (optopt))
               				fprintf (stderr, "Unknown option `-%c'.\n", optopt);
             			else
               				fprintf (stderr,
                        	"Unknown option character `\\x%x'.\n", optopt);
            			return 0;
             default:
             			return 0;
          }
     if(!iarg || !parg || !carg || !darg || !marg)
     	return 0;
     return i;
}
